/*=====================================================================================
   Program Name: vtr01
   This is a program that extracts voter transition rates in a multi-party setting
   from from binary relationships using base programs such as ecol4.  

Program written by Won-ho Park, University of Michigan
Copyright Won-ho Park, Sept. 2002 
=======================================================================================

  The input arguments are two sets of vote returns (which means not fractions),
  devided by a "|", the first set being from the first election. Thus, the command
  syntax would be 

  vtr01 party1_election1 party2_election1 ... | party1_election2 party2_election2... , options
========================================================================================*/

program define vtr01, rclass byable(recall)
	version 7.0
	syntax varlist(min=2 numeric) [if] [in] , SECond(varlist numeric min=2) [ecol(integer 4) BINary(integer 0)]
	marksample touse, strok
		if (`ecol'==1){
		local type "Logit"
		}
		else{
		local type "Probit"
		}

*********** Pre-DEFINE Temporary Names Here **********************************************

	tempname T T2 Corr nofp_1 nofp_2 natsum1 natsum2 tot1 total natsum22 NofObs pops props
* Get the national proportions: 
	quietly tabstat `varlist' if `touse', s(sum) save
	mat `natsum1' = r(StatTot)
*	mat list  `natsum1'
	quietly tabstat `second'  if `touse', s(sum) save
	mat `natsum2' = r(StatTot)

* Define the temporal variable that adds up the relevant parties across observations
	tempvar total1 total2
	qui egen `total1'= rsum(`varlist')
	qui egen `total2'= rsum(`second')

* Here, store the list of the 1st parties in local macros, "pi_e1"
	tokenize `varlist'
	local i=1
	while "``i''"~="" {
		local p`i'_e1  "``i''"
*		di "`p`i'_e1'"
		local i = `i' + 1
	}
	scalar `nofp_1' = `i' - 1
*	di `nofp_1'

* Now, store the list of the 2nd election parties in local macros, "pi_e2"
	tokenize `second'
	local i=1
	while "``i''"~="" {
		local p`i'_e2  "``i''"
*		di "`p`i'_e2'"
		local i = `i' + 1
	}
	scalar `nofp_2' = `i'-1
*	di `nofp_2'

* define the output matrix T
	mat `T' = J(`nofp_2', `nofp_1', 0)
	mat colname  `T' = `varlist'
	mat rownames `T' = `second'
	mat `Corr' = `T'
	mat `NofObs'= `T'
*	mat list `T'
*	di "`varlist'" 
*	di "`second'"
		if (`binary' == 1){
		local i = 1
		local j = 1
		qui ecol`ecol' `p`i'_e1' `total1' `p`j'_e2' `total2' if `touse'
		tempname rout corr n votes votes1 votes2 rout2 output
		mat `rout'=r(out)
		sca `corr'=r(rho)
		sca `n'   =r(n)
*		mat list `rout'
		di _newline "{it:Thomsen Estimates, `type'}"
		di in text "{hline 20}{c TT}{hline 45}"
		di "Voter Transfer Rates" _col(21)"{c |}" _col(30) "`p1_e1'"    _col(52) "`p2_e1'"
		di "{txt}{hline 20}{c +}{hline 45}"
		di "{txt}{ralign 19: `p1_e2'}" _col(21)"{c |}{res}" _col(29) %7.5f `rout'[1,1] " {space 14} "  %7.5f `rout'[2,1]
		di "{txt}"_col(21) "{c |}{res} [" %7.5f `rout'[1,2] ", "  %7.5f `rout'[1,3] "]{space 5}[" %7.5f `rout'[2,3] ", "  %7.5f `rout'[2,2]  "]"
		di "{txt}{hline 20}{c +}{hline 45}"
		di "{txt}{ralign 19: `p2_e2'}" _col(21)"{c |}{res}" _col(29) %7.5f 1-`rout'[1,1]  " {space 14} "  %7.5f 1-`rout'[2,1] 
		di "{txt}"_col(21) "{c |}{res} [" %7.5f 1-`rout'[1,3] ", "  %7.5f 1-`rout'[1,2] "]{space 5}[" %7.5f 1-`rout'[2,2] ", "  %7.5f 1-`rout'[2,3] "]"
		di in text "{hline 20}{c BT}{hline 45}"
		di _col(7) "Correlation = {res}" %7.5f `corr'    _col(40) "{txt}Number of Observations = {res}" `n'
		mat `pops' = r(Pops)
		mat `props'= r(Props)
		return matrix Pops  `pops'
		return matrix Props `props'
		return scalar n=`n'
		exit
		}

	* Now run r by c times of binary tranfer programs
	local c = `nofp_1'
	local r = `nofp_2'
		forvalues i = 1/`c'{
			forvalues j = 1/`r'{
			qui ecol`ecol' `p`i'_e1' `total1' `p`j'_e2' `total2' if `touse'
			mat define `T'[`j',`i']=r(core)
			mat define `Corr'[`j',`i']=r(rho)
			mat define `NofObs'[`j', `i']=r(n)
	*		mat list `T'
			}
		}
	matrix `T2'=`T'\ `natsum1'
	* Joint the matrix by column. (Append below, 1st Election)
		sca `tot1'= 0
		forvalues i = 1 / `c'{
			sca `tot1' = `tot1' + `natsum1'[1,`i']
		}
		sca `total' = 0
		forvalues i = 1 / `r'{
			sca `total' = `total' + `natsum2'[1,`i']
		}

	matrix `natsum22'= `natsum2', `total'
	matrix `T2'=`T2',`natsum22''
	return matrix vtrmat `T2'
	return matrix rho `Corr'
	return matrix n `NofObs'
	return matrix vtrates `T'
	return matrix sum1 `natsum1'
	matrix `natsum2' = `natsum2''
	return matrix sum2 `natsum2'
	*return scalar n    = r(n)
	return scalar tot1 = `tot1'
	return scalar tot2 = `total'
	mat list return(vtrmat), t("Voter Transition Rates, `type', Unconditional, Uncorrected")
	*di " n = " return(n) 
	mat list return(n)     , t("Number of Valid Observations")
	mat list return(rho), t("`type' Correlations, Crude Binary")
	*di " totsum = " return(natsum)
end
